<template>
  <div class="pagination">
    <!-- 这里不配合css中的 &[disabled]会不生效？？？？？？？？？？？ 为什么？？？ -->
    <button @click="back" :disabled="pageNo === 1">上一页</button>
    <!-- <button v-if="pageNo > Math.ceil(continues / 2)" @click="sendPageNo(1)">
      1
    </button> -->
    <button v-if="startNumAndEndNum.start > 1" @click="sendPageNo(1)">1</button>
    <!-- <button v-if="pageNo > Math.ceil(continues / 2) + 1">···</button> -->
    <button v-if="startNumAndEndNum.start > 2">···</button>

    <button
      v-for="(page, index) in startNumAndEndNum.end"
      :key="index"
      :class="{ active: page === pageNo }"
      v-show="page >= startNumAndEndNum.start"
      @click="sendPageNo(page)"
    >
      {{ page }}
    </button>

    <!-- <button v-if="pageNo < totalPage - Math.ceil(continues / 2)">···</button> -->
    <button v-if="startNumAndEndNum.end < totalPage - 1">···</button>
    <!-- <button
      v-if="pageNo < totalPage - (Math.ceil(continues / 2) - 1)"
      @click="sendPageNo(totalPage)"
    >
      {{ totalPage }}
    </button> -->
    <button
      v-if="startNumAndEndNum.end < totalPage"
      @click="sendPageNo(totalPage)"
    >
      {{ totalPage }}
    </button>
    <button @click="forword" :disabled="pageNo === totalPage">下一页</button>

    <button style="margin-left: 30px">共 {{ total }} 条</button>
    <!-- 开始{{ startNumAndEndNum.start }}---结束{{ startNumAndEndNum.end }}----当前{{ pageNo }} -->
  </div>
</template>

<script>
export default {
  name: "Pagination",
  // total表示全部有多少条数据，pageNo表示当前页码数，pageSize表示页面展示数，continues表示要展示的连续页码数
  props: ["total", "pageNo", "pageSize", "continues"],
  computed: {
    // 一共有多少页
    totalPage() {
      return Math.ceil(this.total / this.pageSize);
    },
    // 计算起始页码和结束页码
    startNumAndEndNum() {
      const { total, pageNo, totalPage, continues } = this;
      let start = 0,
        end = 0;
      // 当全部页码数小于将要展示的连续页码数时
      if (total < continues) {
        start = 1;
        end = total;
      } else {
        start = pageNo - Math.floor(continues / 2);
        end = pageNo + Math.floor(continues / 2);
      }
      if (start < 1) {
        start = 1;
        end = continues;
      }
      if (end > totalPage) {
        end = totalPage;
        start = totalPage - continues + 1;
      }
      return { start, end };
    },
  },
  methods: {
    // 点击数字
    sendPageNo(pageNo) {
      this.$emit("getPageNo", pageNo);
    },
    // 上一页
    back() {
      this.$emit("getPageNo", this.pageNo - 1);
    },
    // 下一页
    forword() {
      this.$emit("getPageNo", this.pageNo + 1);
    },
  },
};
</script>

<style lang="less" scoped>
.pagination {
  text-align: center;
  // button {
  //     margin: 0 5px;
  //     background-color: #f4f4f5;
  //     color: #606266;
  //     outline: none;
  //     border-radius: 2px;
  //     padding: 0 4px;
  //     vertical-align: top;
  //     display: inline-block;
  //     font-size: 13px;
  //     min-width: 35.5px;
  //     height: 28px;
  //     line-height: 28px;
  //     cursor: pointer;
  //     box-sizing: border-box;
  //     text-align: center;
  //     border: 0;

  //     &[disabled] {
  //       color: #c0c4cc;
  //       // cursor: not-allowed;
  //     }

  //     &.active {
  //       // cursor: not-allowed;
  //       background-color: #409eff;
  //       color: #fff;
  //     }
  //   }

  button {
    margin: 0 5px;
    background-color: #f4f4f5;
    color: #606266;
    outline: none;
    border-radius: 2px;
    padding: 0 4px;
    vertical-align: top;
    display: inline-block;
    font-size: 13px;
    min-width: 35.5px;
    height: 28px;
    line-height: 28px;
    cursor: pointer;
    box-sizing: border-box;
    text-align: center;
    border: 0;
    &[disabled] {
      color: #c0c4cc;
      cursor: not-allowed;
    }
  }
  .active {
    background-color: #409eff;
    color: #fff;
  }
}
</style>
